package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.mapper.SysUserRunLogMapper;
import com.settlement.system.model.entity.SysUserRunLog;
import com.settlement.system.model.form.SysUserRunLogForm;
import com.settlement.system.model.form.UserRunLogForm;
import com.settlement.system.model.query.SysUserRunLogQuery;
import com.settlement.system.model.vo.SysUserRunLogImportVo;
import com.settlement.system.service.SysUserRunLogService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户运行日志表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-03-05
 */
@Service
public class SysUserRunLogServiceImpl extends ServiceImpl<SysUserRunLogMapper, SysUserRunLog> implements SysUserRunLogService {

    @Override
    public Page<SysUserRunLog> getPage(SysUserRunLogQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysUserRunLog> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

    @Override
    public boolean saveSysUserRunLog(SysUserRunLogForm sysUserRunLogForm) {
        SysUserRunLog sysUserRunLog = new SysUserRunLog();
        BeanUtil.copyProperties(sysUserRunLogForm, sysUserRunLog);
        sysUserRunLog.setCreateTime(null);
        return this.saveOrUpdate(sysUserRunLog);
    }

    @Override
    public void saveSysUserRunLog(UserRunLogForm userRunLogForm) {
        SysUserRunLog sysUserRunLog = new SysUserRunLog();
        BeanUtil.copyProperties(userRunLogForm, sysUserRunLog);
        sysUserRunLog.setCreateTime(null);
        this.saveOrUpdate(sysUserRunLog);
    }

    @Override
    public boolean read(Long id) {
        SysUserRunLog sysUserRunLog = this.getById(id);
        // 已读
        sysUserRunLog.setReadStatus(1);
        // 保存
        return this.saveOrUpdate(sysUserRunLog);
    }

    @Override
    public SysUserRunLogForm getSysUserRunLogFormData(Long id) {
        SysUserRunLog sysUserRunLog = this.getById(id);
        SysUserRunLogForm sysUserRunLogForm = new SysUserRunLogForm();
        BeanUtil.copyProperties(sysUserRunLog, sysUserRunLogForm);
        return sysUserRunLogForm;
    }

    @Override
    public boolean deleteSysUserRunLog(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysUserRunLogImportVo> voList) {
        List<SysUserRunLog> sysUserRunLogList = new ArrayList<>();
        for (SysUserRunLogImportVo importVo : voList) {
            SysUserRunLog sysUserRunLog = new SysUserRunLog();
            BeanUtil.copyProperties(importVo, sysUserRunLog);
            sysUserRunLogList.add(sysUserRunLog);
        }
        this.saveOrUpdateBatch(sysUserRunLogList);
    }

    @Override
    public List<SysUserRunLogImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysUserRunLog> sysUserRunLogList = this.getBaseMapper().selectBatchIds(ids);
        List<SysUserRunLogImportVo> voList = new ArrayList<>();
        for (SysUserRunLog sysUserRunLog : sysUserRunLogList) {
            SysUserRunLogImportVo vo = new SysUserRunLogImportVo();
            BeanUtil.copyProperties(sysUserRunLog, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public List<SysUserRunLog> getUserRunLogs(String mobile, Integer pageNumber) {
        SysUserRunLogQuery queryParams = new SysUserRunLogQuery();
        queryParams.setPageSize(50);
        queryParams.setPageNum(pageNumber);
        queryParams.setMobile(mobile);
        Page<SysUserRunLog> sysUserRunLogPage = this.getPage(queryParams);
        return sysUserRunLogPage.getRecords();
    }
}
